package cn.jtfadmin.cms.controller;

import cn.jtfadmin.base.lang.common.domain.dto.*;
import cn.jtfadmin.cms.domain.dto.*;
import cn.jtfadmin.cms.domain.entity.CmsMenuEntity;
import cn.jtfadmin.cms.service.CmsMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.List;
import java.util.Objects;

/**
 * cms 菜单controller
 * @author jtf
 * @since 0.0.2
 */
@RestController
public class CmsMenuController {

    @Autowired
    private CmsMenuService service;


    /**
     * 分页查询
     *
     * @param specPagePDTO 查询条件
     * @return 分页数据
     */
    @GetMapping(value = "/cms/menu/list")
    public ResultDTO<List<TreeRDTO<CmsMenuModelDTO>>> list(SpecPagePDTO<CmsMenuEntity> specPagePDTO) {
        return ResultDTO.successData(service.findTree(specPagePDTO,CmsMenuModelDTO.class));
    }

    /**
     * 字典
     *
     * @param columns 命名列
     * @return 无
     */
    @GetMapping(value = "/cms/menu/named")
    public ResultDTO<List<AntdvTreeDataRDTO>> named(@RequestParam(required = false) String columns,
                                                          @RequestParam(required = false) SpecPDTO<CmsMenuEntity> specPDTO) {
        return ResultDTO.successData(service.findAntdTree(columns, specPDTO));
    }

    /**
     * 新增
     *
     * @param addPDTO 新增参数
     * @return id
     */
    @PostMapping(value = "/cms/menu/add")
    public ResultDTO<String> add(@RequestBody @Validated CmsMenuAddPDTO addPDTO) {
        return ResultDTO.successData(service.add(addPDTO).getId());
    }

    /**
     * 编辑
     *
     * @param editPDTO 查询条件
     * @return 无
     */
    @PutMapping(value = "/cms/menu/edit")
    public ResultDTO<String> edit(@RequestBody @Validated CmsMenuEditPDTO editPDTO) {
        return ResultDTO.successData(service.edit(editPDTO).getId());
    }

    /**
     * 按照id删除
     *
     * @param id id
     * @return 无
     */
    @DeleteMapping(value = "/cms/menu/del")
    public ResultDTO<String> del(@RequestParam() @NotBlank String id) {
        service.deleteById(id);
        return ResultDTO.success();
    }

    /**
     * 批量删除
     *
     * @param ids 查询条件
     * @return 分页数据
     */
    @DeleteMapping(value = "/cms/menu/delBatch")
    public ResultDTO<String> delBatch(@RequestParam() @NotBlank String ids) {
        service.deleteBatch(ids);
        return ResultDTO.success();
    }

    /**
     * 按照id获取contentHtml
     * @param id id
     * @return
     */
    @GetMapping(value = "/cms/menu/getContentHtml", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultDTO<String> getContentHtml(@RequestParam @NotBlank String id){
        String contentHtml = service.findRequiredById(id).getContentHtml();
        return ResultDTO.successData(Objects.isNull(contentHtml)?"":contentHtml);
    }

    /**
     * 按照id获取contentHtml
     * @param id id
     * @return
     */
    @GetMapping(value = "/cms/menu/content/{id}", produces = MediaType.TEXT_HTML_VALUE)
    public String getContent(@PathVariable @NotBlank String id){
        String contentHtml = service.findRequiredById(id).getContentHtml();
        return contentHtml;
    }

}
